package com.aspire.boc.mms.wmf.filter;

import java.io.IOException;
import java.util.Vector;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.xmlrpc.XmlRpcClient;
import org.apache.xmlrpc.XmlRpcException;

import com.aspire.boc.mms.util.ResourceManager;

public class UserAccessFilter implements Filter {
	private Log logger = LogFactory.getLog(UserAccessFilter.class);

	private String camXmlRpcUrl = null;

	private String system_def = null;

	private String user_name = null;

	private String promptPageAddr = null;

	public void init(FilterConfig config) throws ServletException {
		camXmlRpcUrl = config.getInitParameter("XmlRpcServerUrl");
		system_def = config.getInitParameter("system_def");
		promptPageAddr = config.getInitParameter("promptPageAddr");
	}

	public void doFilter(ServletRequest request, ServletResponse response,
			FilterChain chain) throws IOException, ServletException {
		ResourceManager rm = ResourceManager.getInstance();
		// cas写入
		Object userObj = ((HttpServletRequest) request).getSession()
				.getAttribute("edu.yale.its.tp.cas.client.filter.user");
		// 跳转会登陆页面
		if (userObj == null) {
			request.getRequestDispatcher(promptPageAddr).forward(request,
					response);
			return;
		}
		//--------------
		try {
			user_name = (String) userObj;
			Object sessionValue = ((HttpServletRequest) request).getSession()
					.getAttribute("IsExistUserInThisSystem");
			// 用户在本系统中
			if (sessionValue != null && ((String) sessionValue).equals(user_name)) {
				chain.doFilter(request, response);
			} else {
				// 是否在系统中
				if (IsExistUserInThisSystem()) {
					((HttpServletRequest) request).getSession().setAttribute(
							"IsExistUserInThisSystem", user_name);
					chain.doFilter(request, response);
				} else {
					((HttpServletRequest) request).getSession().setAttribute(
							"errorPromptInfo",
							rm.getValue("error.logon.admin.freeze"));
					((HttpServletRequest) request).getRequestDispatcher(
							promptPageAddr).forward(request, response);
				}
				
			}
		} catch (XmlRpcException e) {
			logger.error(e);
		}
	}

	private boolean IsExistUserInThisSystem() throws XmlRpcException,
			IOException {
		if (camXmlRpcUrl == null) {
			camXmlRpcUrl = System.getProperty("XmlRpcServerUrl");
		}
		if (system_def == null) {
			system_def = System.getProperty("system_def");
		}
		Integer flag = -1;
		XmlRpcClient client = new XmlRpcClient(camXmlRpcUrl);
		Vector<Object> params = new Vector<Object>();
		params.add(system_def);
		params.add(user_name);
		flag = (Integer) client.execute("UserService.notExistUser", params);
		if (flag == 2) {
			return true;
		}
		return false;
	}

	public void destroy() {

	}
}
